package com.integral.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.integral.biz.IOrderBiz;
import com.integral.entity.Bank;
import com.integral.entity.BankExchangeRate;
import com.integral.entity.BankType;
import com.integral.entity.Order;
import com.integral.entity.Product;
import com.integral.entity.User;
import com.integral.entity.UserType;
import com.integral.service.BankExchangeRateService;
import com.integral.service.BankService;
import com.integral.service.BankTypeService;
import com.integral.service.OrderService;
import com.integral.service.UserService;
import com.integral.service.UserTypeService;
import com.integral.vo.BankExchangeRateVo;
import com.integral.vo.OrderVo;

/**
 * @Description: 订单表BIZ实现类
 * @author zhaofan
 * @date 2018年05月08日 上午01:48:50
 */
@Service("orderBiz")
public class OrderBiz implements IOrderBiz {
	
	/**
	 * 注入订单表Service
	 */
	@Resource
	private OrderService orderService;
	@Resource
	private UserService userService;
	@Resource
	private UserTypeService userTypeService;
	@Resource
	private BankService bankService;
	@Resource
	private BankTypeService bankTypeService;
	@Resource
	private BankExchangeRateService bankExchangeRateService;
	
	/**
	 * @Description: 新增订单表
	 * @param order
	 * @return 受影响行数
	 * @author zhaofan
	 * @date 2018年05月08日 上午01:48:50
	 */
	public int addOrder(Order order) {
		return orderService.addOrder(order);
	}
	
	/**
	 * @Description: 修改订单表
	 * @param order 订单表
	 * @return 受影响行数
	 * @author zhaofan
	 * @date 2018年05月08日 上午01:48:50
	 */
	public int updateOrder(Order order) {
		return orderService.updateById(order);
	}
	
	/**
	 * @Description: 删除订单表
	 * @param id 主键
	 * @return 受影响行数
	 * @author zhaofan
	 * @date 2018年05月08日 上午01:48:50
	 */
	public int deleteOrder(Long id) {
		return orderService.deleteById(id);
	}
	
	/**
	 * @Description: 根据ID查询订单表
	 * @param id 主键
	 * @return 订单表
	 * @author zhaofan
	 * @date 2018年05月08日 上午01:48:50
	 */
	public Order findById(Long id) {
		return orderService.findById(id);
	}
	
	/**
	 * 查询订单总记录数
	 * @param orderVo
	 * @return
	 */
	public Long findOrderCount(OrderVo orderVo){
		return orderService.findOrderCount(orderVo);
	}
	
	/**
	 * 查询订单列表
	 * @param orderVo
	 * @return
	 */
	public List<OrderVo> findOrder(OrderVo orderVo){
		List<OrderVo> list = orderService.findOrder(orderVo);
		for (OrderVo orderVo2 : list) {
			//编码转换
			codeTransform(orderVo2);
		}
		return list;
	}
	/**
	 * 编码转换
	 */
	private void codeTransform(OrderVo orderVo){
		//转换用户编码
		if(!StringUtils.isEmpty(orderVo.getUserCode())){
			User user = userService.findByCode(orderVo.getUserCode());
			if(null != user){
				orderVo.setUserName(user.getUserName());
				//转换用户类型编码
				UserType userType = userTypeService.findByCode(user.getUserTypeCode());
				if(null != userType){
					orderVo.setUserTypeName(userType.getUserTypeName());
				}
			}
		}
		//转换银行编码
		if(!StringUtils.isEmpty(orderVo.getBankCode())){
			Bank bank = bankService.findByBankCode(orderVo.getBankCode());
			if(null != bank){
				orderVo.setBankName(bank.getBankName());
			}
		}
		//转换银行产品编码
		if(!StringUtils.isEmpty(orderVo.getBankTypeCode())){
			BankType bankType = bankTypeService.findByBankTypeCode(orderVo.getBankTypeCode());
			if(null != bankType){
				orderVo.setBankTypeName(bankType.getBankTypeName());
			}
		}
		//获取汇率信息
		if(null != orderVo.getExchangeRateId() && orderVo.getExchangeRateId() != 0){
			BankExchangeRateVo queryParamet = new BankExchangeRateVo();
			queryParamet.setId(orderVo.getExchangeRateId());
			 BankExchangeRate  bankExchangeRate =bankExchangeRateService.findByBankExchangeRate(queryParamet);
			 if(null != bankExchangeRate){
				 orderVo.setExchangeRate(bankExchangeRate.getBankExchangeRate());
			 }
		}
	}
	
	/**
	 * 根据订单编号修改订单状态
	 * @param order
	 * @return
	 */
	public int updateByStatus(Order order){
		return orderService.updateByStatus(order);
	}
	
	/**
	 * 根据ID查询 支付产品信息
	 * @param product
	 * @return
	 */
	public Product findProductById(int productId){
		return orderService.findProductById(productId);
	}
}